<template>
  <div>
    <a-col :xl="18" :lg="16" :md="20" :sm="24" style="float: right;">
      <div class="two-login">
        <div class="main">
          <h3 class="new_tit">用户登录</h3>
          <a-form-model class="user-layout-login" @keyup.enter.native="handleSubmit">
            <login-account ref="alogin" @validateFail="validateFail" @senSucess="senSucess" @success="requestSuccess" @fail="requestFailed"></login-account>
            <a-form-item>
              <a-button size="large" block type="primary"  htmlType="submit"  class="login-button"  :loading="loginBtn"  @click.stop.prevent="handleSubmit" :disabled="loginBtn">
                <span style="float: left;margin-top: 3px;">登录</span>
                <span style="float: right;font-size: 20px;">→</span>
              </a-button>
            </a-form-item>
            <a-form-model-item class="password-field" >
              <router-link :to="{ name: 'alteration'}" class="forge-password">
                忘记密码
              </router-link>
            </a-form-model-item>
          </a-form-model>
          <two-step-captcha v-if="requiredTwoStepCaptcha" :visible="stepCaptchaVisible" @success="stepCaptchaSuccess" @cancel="stepCaptchaCancel"></two-step-captcha>
          <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant>
          <third-login ref="thirdLogin"></third-login>
        </div>
      </div>
    </a-col>
  </div>

</template>

<script>
  import Vue from 'vue'
  import { ACCESS_TOKEN ,ENCRYPTED_STRING} from "@/store/mutation-types"
  import ThirdLogin from './third/ThirdLogin'
  import LoginSelectTenant from "./LoginSelectTenant"
  import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
  import { encryption , getEncryptedString } from '@/utils/encryption/aesEncrypt'
  import { timeFix } from "@/utils/util"
  import { httpAction, getAction } from '@/api/manage'
  import LoginAccount from './LoginAccount'
  import LoginPhone from './LoginPhone'

  export default {
    components: {
      LoginSelectTenant,
      TwoStepCaptcha,
      ThirdLogin,
      LoginAccount,
      LoginPhone
    },
    data () {
      return {
        customActiveKey: 'tab1',
        rememberMe: true,
        loginBtn: false,
        requiredTwoStepCaptcha: false,
        stepCaptchaVisible: false,
        encryptedString:{
          key:"",
          iv:"",
        },
      }
    },
    created() {
      Vue.ls.remove(ACCESS_TOKEN)
      this.getRouterData();
      this.rememberMe = true;
    },
    methods:{
      handleTabClick(key){
        this.customActiveKey = key
      },
      handleRememberMeChange(e){
        this.rememberMe = e.target.checked
      },
      /**跳转到登录页面的参数-账号获取*/
      getRouterData(){
        this.$nextTick(() => {
          let temp = this.$route.params.username || this.$route.query.username || ''
          if (temp) {
            this.$refs.alogin.acceptUsername(temp)
          }
        })
      },


      //登录
      handleSubmit () {
        this.loginBtn = true;
        if (this.customActiveKey === 'tab1') {
          // 使用账户密码登录
          this.$refs.alogin.handleLogin(this.rememberMe)
        } else {
          //手机号码登录
          this.$refs.plogin.handleLogin(this.rememberMe)
        }
      },
      // 校验失败
      validateFail(){
        this.loginBtn = false;
      },

      senSucess(message){
        this.$message.success(message);
      },
      // 登录后台成功
      requestSuccess(loginResult){
        this.$refs.loginSelect.show(loginResult)
      },
      //登录后台失败
      requestFailed (err) {
        let description = ((err.response || {}).data || {}).message || err.message || "请求出现错误，请稍后再试"
        this.$notification[ 'error' ]({
          message: '登录失败',
          description: description,
          duration: 4,
        });
        //账户密码登录错误后更新验证码
        if(this.customActiveKey === 'tab1' && description.indexOf('密码错误')>0){
          this.$refs.alogin.handleChangeCheckCode()
        }
        this.loginBtn = false;
      },
      loginSelectOk(){
        this.loginSuccess()
      },
      //登录成功
      loginSuccess () {
        this.$router.push({ path: "/dashboard/analysis" }).catch(()=>{
          console.log('登录跳转首页出错,这个错误从哪里来的')
        })
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`,
        });
      },

      stepCaptchaSuccess () {
        this.loginSuccess()
      },
      stepCaptchaCancel () {
        this.Logout().then(() => {
          this.loginBtn = false
          this.stepCaptchaVisible = false
        })
      },
      //获取密码加密规则
      getEncrypte(){
        var encryptedString = Vue.ls.get(ENCRYPTED_STRING);
        if(encryptedString == null){
          getEncryptedString().then((data) => {
            this.encryptedString = data
          });
        }else{
          this.encryptedString = encryptedString;
        }
      }

    }

  }
</script>
<style lang="less" scoped>
  .password-field{
    width: 280px;
    margin: 0 auto;
    margin-top: -18px;
    .forge-password{
      float: right;
      color: #1E9FFF;
    }
  }
  .two-login{
    //background:rgba(255,255,255,0.4);
    border-radius: 16px;
    padding: 40px;
    width: 360px;
    margin-top: 100px;
    background-color:#fbfbfb;//lightgray
    box-shadow: 5px 5px  25px #888888;
  }
  .user-layout-login {

    label {
      font-size: 14px;
    }
  .getCaptcha {
      display: block;
      width: 100%;
      height: 40px;
    }

  .forge-password {
      font-size: 14px;
    }

    button.login-button {
      padding: 0 15px;
      font-size: 16px;
      height: 40px;
      width: 100%;
      background-color: #3a72ff;
      border: none;
      /*width: 140px;*/
      margin: 0 auto;
      display: block;
    }

  .user-login-other {
      text-align: left;
      margin-top: 24px;
      line-height: 22px;

    .item-icon {
        font-size: 24px;
        color: rgba(0,0,0,.2);
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color .3s;

      &:hover {
          color: #1890ff;
        }
      }

    .register {
        float: right;
      }
    }
  }

  .logo{
    display: block;
    margin: 0 auto 28px;
    width: 100px;
  }
  .new_tit{
    font-size: 28px;
    text-align: center;
    color: #b8b8b8;
    position: relative;
    margin-bottom: 30px;
    /*&:before{
      content: 'LOGIN';
      color: rgba(255,255,255,0.4);
      font-size: 50px;
      font-weight: 600;
      left: 0;
      right: 0;
      position: absolute;
      top: -25px;
    }*/
    &:after{
      content: '';
      display: block;
      width: 50px;
      height: 3px;
      background-color: #fff;
      left: 50%;
      margin-left: -25px;
      position: absolute;
      bottom: -6px;
    }
  }
  .top {
    text-align: center;
    .header {
      height: 60px;
      line-height: 44px;

      .badge {
        position: absolute;
        display: inline-block;
        line-height: 1;
        vertical-align: middle;
        margin-left: -12px;
        margin-top: -10px;
        opacity: 0.8;
      }

      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
        border-style: none;
      }

      .title {
        color: #fff;
        text-shadow: 4px 4px 4px rgba(0, 0, 0, 0.3);
        font-size: 46px;
        font-weight: 600;
        margin-bottom: 0;
      }
      .tit_sm{
        color: #fff;
        text-shadow: 4px 4px 4px rgba(0, 0, 0, 0.3);
        font-size: 22px;
        font-weight: 600;
      }
    }
    .desc {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
</style>
<style>
  .valid-error .ant-select-selection__placeholder{
    color: #f5222d;
  }
</style>